package com.example.servicegateway.config;

import com.example.servicegateway.loadbalancer.GrayServiceInstanceListSupplier;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 灰度负载均衡配置
 * 这里不能使用@Configuration，这样会使该配置成为全局配置，导致serviceID为NULL，因为本身就是靠environment区分contextName来设置serviceID。
 * 必须使用@LoadBalancerClient指定服务使用灰度负载均衡器或者使用@LoadBalancerClients定义全局配置
 * @author K
 */
public class GrayLoadBalancerConfig {

    @Bean
    public ServiceInstanceListSupplier serviceInstanceListSupplier(ConfigurableApplicationContext context) {
        ReactiveDiscoveryClient reactiveDiscoveryClient = context.getBean(ReactiveDiscoveryClient.class);
        return new GrayServiceInstanceListSupplier(reactiveDiscoveryClient, context.getEnvironment());
    }
}
